package com.example.handler.excelhander;


import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import com.example.constant.CommonConstant;
import org.apache.poi.common.usermodel.HyperlinkType;
import org.apache.poi.ss.usermodel.*;

public class CustomCellWriteHandler extends AbstractCellWriteHandler {

    @Override
    public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, CellData cellData, Cell cell, Head head, int i, boolean b) {
        Workbook workbook = writeSheetHolder.getSheet().getWorkbook();
        CellStyle cellStyle = workbook.createCellStyle();
        if (cell.getRowIndex()== 0){

            if (CommonConstant.returnStr.equals(cell.getStringCellValue())){
                CreationHelper helper = workbook.getCreationHelper();
                Hyperlink hyperlink = helper.createHyperlink(HyperlinkType.DOCUMENT);
                hyperlink.setAddress(CommonConstant.firstName+ CommonConstant.endValue);
                cell.setHyperlink(hyperlink);

                cellStyle.setFillForegroundColor(IndexedColors.BLUE.getIndex());
            }else {
                cellStyle.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
            }

            Font cellFont = workbook.createFont();
            cellFont.setBold(true);
            cellFont.setFontHeight((short)234);
            cellStyle.setFont(cellFont);
            cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        }

        //上边框线条类型
        cellStyle.setBorderTop(BorderStyle.THIN);
        //右边框线条类型
        cellStyle.setBorderRight(BorderStyle.THIN);
        //下边框线条类型
        cellStyle.setBorderBottom(BorderStyle.THIN);
        //左边框线条类型
        cellStyle.setBorderLeft(BorderStyle.THIN);
        cell.setCellStyle(cellStyle);
    }
}